/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function (a, b) {
  let ca = 0
  let res = ''
  for (let i = a.length - 1, j = b.length - 1; i >= 0 || j >= 0; i--, j--) {
    let anum = parseInt(a.charAt(i))
    let bnum = parseInt(b.charAt(j))
    isNaN(anum) && (anum = 0)
    isNaN(bnum) && (bnum = 0)
    let resnum = 0
    if (anum + bnum > 1) {
      resnum = 0
      ca && resnum++
      ca = 1
    } else {
      resnum = anum + bnum
      ca && resnum++
      if (resnum > 1) {
        resnum = 0
        ca++
      } else {
        ca = 0
      }
    }
    res = resnum + res
  }
  return ca ? '1' + res : res
}
console.log(addBinary('11', '1'))
